﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Common;

namespace MxWeiXinPF.Web.shopmgr.coupon
{
    public partial class coupon_shop : MxWeiXinPF.Web.UI.ManagePage
    {
        protected MxWeiXinPF.Model.manager admin_info;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
               // base.ChkAdminLevel("order_list_wxpay", MXEnums.ActionEnum.View.ToString());
                this.admin_info = base.GetAdminInfo();
                this.TreeBind();
                this.RptBind(""," id asc ");
            }
        }

        private void RptBind(string _strWhere, string _orderby)
        {
            BLL.wx_shop_coupon bll = new BLL.wx_shop_coupon();

            Model.manager weixin = base.GetAdminInfo();
            DataSet ds = bll.GetList(" status = 1 and timeend >'" + DateTime.Now.ToString() + "' ");
            this.ddlCouponId.Items.Clear();
            this.ddlCouponId.Items.Add(new ListItem("未选择", "0"));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["couponname"].ToString().Trim();
                this.ddlCouponId.Items.Add(new ListItem(Title, Id));
            }
        }
        private void TreeBind()
        {
            try
            {
                Model.manager weixin = base.GetAdminInfo();
                BLL.manager bll = new BLL.manager();
                DataSet ds = new DataSet();
                if (weixin.agentLevel == 0)
                {
                    if (weixin.id == 39)
                    {
                        ds = bll.GetList(-1, "", " id asc");
                    }
                    else
                    {
                        ds = bll.GetList(-1, " id = " + weixin.id + " ", " id asc");
                    }
                }
                else
                {
                    if (weixin.agentLevel == 1)
                    {
                        ds = bll.GetListByCounty(string.IsNullOrEmpty(weixin.county) ? 0 : int.Parse(weixin.county));
                    }
                    else if (weixin.agentLevel == 2)
                    {
                        ds = bll.GetListByCity(string.IsNullOrEmpty(weixin.city) ? 0 : int.Parse(weixin.city));
                    }
                    else if (weixin.agentLevel == 3)
                    {
                        ds = bll.GetListByProvince(string.IsNullOrEmpty(weixin.province) ? 0 : int.Parse(weixin.province));
                    }
                    else
                    {
                        return;
                    }
                }

                this.ddlShops.Items.Clear();
                this.ddlShops.Items.Add(new ListItem("所有店铺", "0"));
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    ddlShops.Items.Add(new ListItem(ds.Tables[0].Rows[i]["real_name"].ToString(), ds.Tables[0].Rows[i]["id"].ToString()));

                    //if (ds.Tables[0].Rows[i]["id"].ToString() == this.selectshopid.ToString())
                    //{
                    //    this.ddlShops.SelectedValue = ds.Tables[0].Rows[i]["id"].ToString();
                    //}
                }
            }
            catch (Exception ex)
            {

            }
        }


        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (this.ddlCouponId.SelectedValue == "0")
            {
                base.JscriptMsg("送券失败,用户领券数量大于设定值！", "coupon_shop.aspx" , "Error");
                return;
            }
            DataSet ds = new BLL.users().GetList(" shopid = " + this.ddlShops.SelectedValue.ToString());
            int nsucc = 0,nfailed = 0;
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow dr = ds.Tables[0].Rows[i];
                if (SendTicket(dr["id"].ToString()))
                {
                    nsucc++;

                }
                else
                {
                    nfailed++;
                }
            }
            base.JscriptMsg(string.Format("送券结果,成功个数:{0},失败个数{1}",nsucc,nfailed), "coupon_shop.aspx", "Error");
        }

        private bool SendTicket(string userid)
        {
            BLL.users buser = new BLL.users();
            string couponid = this.ddlCouponId.SelectedValue;
            BLL.wx_shop_coupon bcoupon = new BLL.wx_shop_coupon();
            Model.wx_shop_coupon mcoupon = bcoupon.GetModel(int.Parse(couponid));
            Model.users muser = buser.GetModel(Convert.ToInt32(userid));
            BLL.wx_shop_coupon_data bcoupondata = new BLL.wx_shop_coupon_data();
            if (muser != null && mcoupon != null)
            {
                DataSet ds = bcoupondata.GetList(string.Format("senduid = {0} and couponid={1} ", userid, mcoupon.id));
                if (ds == null || ds.Tables[0].Rows.Count == 0)
                {
                    List<Model.wx_shop_coupon_data> datas = bcoupondata.DataTableToList(ds.Tables[0]);
                    if (datas.Count >= mcoupon.numperuser)
                    {
                        return false;
                    }
                    else
                    {
                        Model.wx_shop_coupon_data thecoupon = new Model.wx_shop_coupon_data();
                        thecoupon.couponid = mcoupon.id;
                        thecoupon.shopid = base.GetAdminInfo().id;
                        thecoupon.openid = muser.wxOpenId;
                        thecoupon.gettype = 0;
                        thecoupon.status = 0;
                        thecoupon.gettime = DateTime.Now;
                        thecoupon.usetime = DateTime.Now;
                        thecoupon.senduid = muser.id;
                        thecoupon.ordersn = 0;
                        thecoupon.sendmsgstate = 0;
                        thecoupon.sendmsgtime = DateTime.Now;

                        if (bcoupondata.Add(thecoupon) > 0)
                        {

                            //送券成功,增加日志记录
                            Model.wx_shop_coupon_log log = new Model.wx_shop_coupon_log();
                            log.shopid = base.GetAdminInfo().id;
                            log.openid = muser.wxOpenId;
                            log.times = DateTime.Now;
                            log.couponid = mcoupon.id;
                            log.orderid = 0;
                            log.explain = "后台赠送用户";
                            log.remark = " ";
                            log.exstr1 = " ";
                            new BLL.wx_shop_coupon_log().Add(log);
                            return true;
                        }

                    }
                }
            }
            return false;
        }
    }
}